package com.monlyu.bo;

import java.util.Date;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.monlyu.bean.Page;
import com.monlyu.bean.Pages;
import com.monlyu.util.DateUtil;
import com.monlyu.util.StringUtil;
import com.monlyu.vo.BroadcastError;
import com.monlyu.vo.User;

/*******************************************************************************
 * 
 * <b>功能描述☞</b>新闻播出差错管理<br>
 * 
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>news<br>
 * <b>文件路径☞</b>com.monlyu.bo.BroadcastErrorBo.java<br>
 * <b>开发日期☞</b>2010-2-22 <br>
 * 
 * @author chendan
 * @since JDK 5
 * @version 1.0.8
 * 
 ******************************************************************************/
@Repository(value = "broadcastErrorBo")
public class BroadcastErrorBo extends BaseBo {

	/**
	 * 新建新闻播出的差错信息
	 * 
	 * @param broadcastError
	 * @since chendan 2010-2-22
	 */
	public void createBroadcaseError(BroadcastError broadcastError) {
		broadcastError.setRecorder(curUser());
		broadcastError.setRecordTime(DateUtil.now());
		dao.insertOrUpdate(broadcastError);

	}

	/**
	 * 维护新闻播出的差错信息
	 * 
	 * @param broadcastError
	 * @since chendan 2010-2-22
	 */
	public void updateBroadcaseError(BroadcastError broadcastError) {
		BroadcastError dbBE = getBroadcaseError(broadcastError.getId());
		dbBE.setDate(broadcastError.getDate());
		dbBE.setDetail(broadcastError.getDetail());
		dbBE.setErrorType(broadcastError.getErrorType());
		// dao.load(broadcastError.getUser());
		// dbBE.setUser(broadcastError.getUser());
		dao.insertOrUpdate(dbBE);
	}

	/**
	 * 根据ID查差错信息
	 * 
	 * @param id
	 * @return
	 * @since chendan 2010-2-22
	 */
	public BroadcastError getBroadcaseError(Long id) {
		return (BroadcastError) dao.get(BroadcastError.class, id);
	}

	/**
	 * 查询播出的差错信息
	 * 
	 * @param broadcastError
	 * @param endDate
	 *            结束时间
	 * @param beginDate
	 *            开始时间
	 * @return
	 * @since chendan 2010-2-22
	 */
	public Pages<BroadcastError> searchBE(Page page,
			BroadcastError broadcastError, Date beginDate, Date endDate) {
		Criteria criteria = dao.initCriteria(BroadcastError.class);
		if (broadcastError != null) {
			if (!StringUtil.isEmpty(broadcastError.getErrorType())) {
				criteria.add(Restrictions.eq("errorType", broadcastError
						.getErrorType()));
			}
			if (!StringUtil.isEmpty(broadcastError.getDetail())) {
				criteria.add(Restrictions.like("detail", broadcastError
						.getDetail(), MatchMode.ANYWHERE));
			}
			if (broadcastError.getUser() != null) {
				User user = broadcastError.getUser();
				if (!StringUtil.isEmpty(user.getName())) {
					criteria.createAlias("user", "user");
					criteria.add(Restrictions.or(Restrictions.like("user.name",
							user.getName().trim(), MatchMode.ANYWHERE),
							Restrictions.like("user.nickName", user.getName()
									.trim(), MatchMode.ANYWHERE)));
				}
			}
		}
		if (beginDate != null) {
			criteria.add(Restrictions.ge("date", beginDate));
		}
		if (endDate != null) {
			criteria.add(Restrictions.le("date", DateUtil.addDay(endDate,1)));
		}
		return findByCriteria(page.getNo(), page.getSize(), criteria);
	}

	/**
	 * 删除差错信息
	 * 
	 * @param id
	 * @since chendan 2010-2-22
	 */
	public void delete(Long id) {
		BroadcastError broadcastError = getBroadcaseError(id);
		if (broadcastError != null) {
			delete(BroadcastError.class, id);
		}

	}
}
